var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxUserExtInfoRModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `user_id` bigint(20) unsigned NOT NULL COMMENT '用户id',
    // `info_id` bigint(20) NOT NULL COMMENT '信息id',
    // `type` int(3) NOT NULL DEFAULT '0' COMMENT '信息分类(同yjx_cd_info.type)',
    // `state` int(2) NOT NULL DEFAULT '30' COMMENT '状态: 0无效, 10仅个人可见, 20部分人可见, 30公开信息',
    // `order` int(3) NOT NULL DEFAULT '999' COMMENT '用户自定义排序',
    // `desc` varchar(200) DEFAULT NULL COMMENT '备注',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `update_by` varchar(50) NOT NULL DEFAULT '',
    // `del_flg` int(1) NOT NULL DEFAULT '0'

};

YjxUserExtInfoRModel.prototype = new YjxBaseModel();

// 取得表名
YjxUserExtInfoRModel.prototype.getTableName = function(){
    return 'yjx_user_ext_info_r';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxUserExtInfoRModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'user_id='+this.user_id;
};

/**
 * 删除用户指定类型的所有扩展信息
 *
 * @author 王俊
 * @date 2018-06-19
 *
 * @param {Object} options
 * @param {Number} options.type     扩展信息类型: 101:职业, 102:特长'
 * @param {Number} options.userId    用户唯一标识
 * @param {Function} callback
 */
YjxUserExtInfoRModel.prototype.removeUserAllExtendInfoByType = function (options, callback) {
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxActivityModel.js [getActivityListCnt] getConErr' + getConErr);
            return callback(getConErr, null);
        }
        var sql =
            ' UPDATE yjx.yjx_user_ext_info_r r ' +
            ' SET r.del_flg = 1 ' +
            ' WHERE r.user_id = ' + options.userId +
            '   AND r.type = ' + options.type +
            '   AND r.del_flg = 0 ';
        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxActivityModel.js [getActivityListCnt] queryErr' + queryErr)
            }
            return callback(queryErr, results);
        });
    });
};


// 根据用户id获取最新的用户地址信息
YjxUserExtInfoRModel.prototype.getUserExtInfo = function(userId, infoType, callback){
    var THIS = this;

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('YjxUserExtInfoRModel.getUserExtInfo Err:'+err);
            return callback(err,null);
        }

        var sql = " SELECT t.user_id, t.state, t.info_id, t2.info_cd, t2.type, t2.type_name, " +
            " t2.value, t2.num_value, t2.state AS info_state "+
            ' FROM yjx.yjx_user_ext_info_r t, yjx.yjx_cd_info t2 '+
            " WHERE t.user_id="+ userId +" AND t.del_flg=0 AND t.state>0 "+
            ' AND t.info_id=t2.info_id AND t2.type='+ infoType +
            ' AND t2.del_flg=0 AND t2.state>0 ' +
            ' ORDER BY t.order, t2.order, t.update_time DESC ';
        console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            console.log();
            if(error){
                console.log(error);
                return callback(error,null);
            }
            if(!results || results.length==0){
                return callback(null, null);
            }

            return callback(null, results);
        });
    });
};



module.exports = YjxUserExtInfoRModel;

